package com.nit.store.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.nit.store.mapper.AddressMapper;
import com.nit.store.mapper.CartMapper;
import com.nit.store.mapper.OrderItemMapper;
import com.nit.store.mapper.OrderMapper;
import com.nit.store.pojo.dto.output.CartDto;
import com.nit.store.pojo.dto.output.OrderDto;
import com.nit.store.pojo.entity.Address;
import com.nit.store.pojo.entity.Cart;
import com.nit.store.pojo.entity.Order;
import com.nit.store.pojo.entity.OrderItem;
import com.nit.store.service.OrderService;
import com.nit.store.util.CommonUtils;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author J1ay
 * @Date 2022/7/21 20:51
 * @Description
 */
@Slf4j
@Service
public class OrderServiceImpl implements OrderService {

    /**
     * 地址信息
     */
    @Autowired
    private AddressMapper addressMapper;

    /**
     * 订单信息
     */
    @Autowired
    private OrderMapper orderMapper;

    /**
     * 购物车信息
     */
    @Autowired
    private CartMapper cartMapper;

    @Autowired
    private OrderItemMapper orderItemMapper;

    /**
     * 创建订单
     * @param aid aid
     * @param cids cid数组
     * @param uid uid
     * @param username username
     */

    @Override
    public void createOrder(Long aid, Long[] cids, long uid, String username) {
        /**
         * 创建订单
         */
        Order orderEntity = new Order();
        orderEntity.setUid(uid);
        orderEntity.setCreatedUser(username);
        orderEntity.setModifiedUser(username);
        orderEntity.setStatus(0L);

        /**
         * 创建地址
         */

        Address addressEntity = addressMapper.selectById(aid);
        orderEntity.setRecvProvince(addressEntity.getProvinceName());
        orderEntity.setRecvCity(addressEntity.getCityName());
        orderEntity.setRecvArea(addressEntity.getAreaName());
        orderEntity.setRecvAddress(addressEntity.getAddress());
        orderEntity.setRecvName(addressEntity.getReceiver());
        orderEntity.setRecvPhone(addressEntity.getPhone());
        orderEntity.setOrderTime(LocalDateTime.now());

        CartDto[] cartDtos = cartMapper.getCartByCid(cids);
        // 购物车内的商品
        OrderItem[] orderItemEntities = new OrderItem[cartDtos.length];
        long totalPrice = 0;
        for (int i = 0; i < cartDtos.length; i++) {
            totalPrice += cartDtos[i].getRealPrice() * cartDtos[i].getNum();
            orderItemEntities[i] = new OrderItem();
            // 图片
            orderItemEntities[i].setImage(cartDtos[i].getImage());
            // 购买数量
            orderItemEntities[i].setNum(cartDtos[i].getNum());
            // 商品
            orderItemEntities[i].setPid(cartDtos[i].getPid());
            orderItemEntities[i].setPrice(cartDtos[i].getRealPrice());
            orderItemEntities[i].setTitle(cartDtos[i].getTitle());
            orderItemEntities[i].setCreatedUser(username);
            orderItemEntities[i].setModifiedUser(username);
        }
        // 总价格
        orderEntity.setPrice(totalPrice);
        orderMapper.insert(orderEntity);

        for (OrderItem item : orderItemEntities) {
            item.setOid(orderEntity.getOid());
            orderItemMapper.insert(item);
        }

        UpdateWrapper<Cart> wrapper = new UpdateWrapper<>();
        wrapper.in("cid", Arrays.asList(cids));
        cartMapper.delete(wrapper);
    }


    /**
     * 个人订单详情
     * @param status
     * @param uid
     * @return
     */
    @Override
    public List<OrderDto> getOrderList(int status, Long uid) {
        QueryWrapper<Order> queryWrapper = new QueryWrapper();
        queryWrapper.eq("uid", uid);
        // -1 代表查询全部订单
        if (status != -1) {
            queryWrapper.eq("status", status);
        }
        List<Order> orders = orderMapper.selectList(queryWrapper);
        List<OrderDto> orderDtoList = CommonUtils.getMapperFacade().mapAsList(orders, OrderDto.class);
        for (OrderDto orderDto : orderDtoList) {
            QueryWrapper<OrderItem> itemQueryWrapper = new QueryWrapper();
            List<OrderItem> orderItem = orderItemMapper.selectList(itemQueryWrapper.eq("oid", orderDto.getOid()));
            orderDto.setOrderItem(orderItem);
        }
        return orderDtoList;
    }

    /**
     * 删除订单操作
     * @param oid
     */
    @Override
    public void delete(long oid) {
        orderMapper.deleteById(oid);
    }

    @Override
    public void deleteItem(long oid, long pid) {
        QueryWrapper<OrderItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("oid", oid).eq("pid", pid);
        orderItemMapper.delete(queryWrapper);
        QueryWrapper<OrderItem> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("oid",oid);
        // 该订单已经空了
        if (0 == orderItemMapper.selectList(queryWrapper1).size()) {
            delete(oid);
        }
    }
}
